home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 152 / samples.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-21  |  13.3 KB  |  427 lines

  1. /***************************************************************************/
  2. /* samples.c  modified for in-line resource file using rscfix approach     */
  3. /*  C-manship, Listing 1,ST-Log #13,Megamax C.(refined for Mark Williams C)*/
  4. /**************************************************CJPurcell*11June1987*****/
  5. #include <OSBIND.H>
  6. #include <obdefs.h>
  7. #include <gemdefs.h>
  8. #include <portab.h>
  9.  
  10. #include "samples.h"         /* The header file from RSC   */
  11. #include "samples.rsh"       /* The C source file from RSC */
  12.  
  13. #define FMD_START  0
  14. #define FMD_GROW   1
  15. #define FMD_SHRINK 2
  16. #define FMD_FINISH 3
  17. #define R_TREE     0
  18. #define FINGER     3
  19.  
  20. /* The usual required GEM global arrays */
  21. int work_in[11], 
  22.     work_out[57], 
  23.     pxyarray[10],
  24.     contrl[12], 
  25.     intin[128], 
  26.     ptsin[128], 
  27.     intout[128], 
  28.     ptsout[128];
  29.  
  30. /* Global variables */
  31. int handle, dum;
  32.  
  33. int dial_x,  /* Dialog x coordinate.    */
  34.     dial_y,  /* Dialog y coordinate.    */
  35.     dial_w,  /* Dilaog width.           */
  36.     dial_h,  /* Dialog height.          */
  37.     num,     /* Value of number option. */
  38.     n_x,     /* NUMBERS object x coord. */
  39.     n_y;     /* NUMBERS object y coord. */
  40.  
  41. char number_str[13] = "    0000    ";  /* NUMBERS string. */
  42.  
  43. char *find_str();  /* Function declaration. */
  44.  
  45. OBJECT *tree;                 /* Pointer to an object tree  */
  46.  
  47. /* #include <rscfix.c>        / * Code to fixup resource     */
  48. /*      RSCFIX.C        09/16/86                Ric Clayton     */
  49. /*      STCREATE.c      04/20/85                ATARI           */
  50. /*      RSCREATE.C      05/18/84 - 11/01/84     LKW             */
  51. /*      Fix something   12/10/84 - 12/10/84     Derek Mui       */
  52. /*      Fix the  size   12/17/84                                */
  53. /*      For ST          04/20/85                Derek Mui       */
  54. /*      For code resident rsc 09/16/86          Ric Clayton     */
  55.  
  56. #define adj(xywh, siz)  (siz * (xywh & 0x00FF) + (xywh >> 8))
  57.  
  58. rsc_fix()
  59. {
  60.         fix_trindex();
  61.         fix_objects();
  62.         fix_tedinfo();
  63.         fix_iconblk();
  64.         fix_bitblk();
  65.         fix_frstr();
  66.         fix_frimg();
  67. }
  68.  
  69. fix_trindex()
  70.         {
  71.         int     test, ii;
  72.  
  73.         for (ii = 0; ii < NUM_TREE; ii++)
  74.                 {
  75.                 test = (int) rs_trindex[ii];
  76.                 rs_trindex[ii] = (long) &rs_object[test];
  77.                 }
  78.         }
  79.  
  80. fix_objects()
  81.         {
  82.         int     test, ii;
  83.         int     wchar, hchar;
  84.  
  85.         graf_handle( &wchar, &hchar, &ii, &ii );
  86.  
  87.         for (ii = 0; ii < NUM_OBS; ii++)
  88.                 {
  89.                 rs_object[ii].ob_x = adj(rs_object[ii].ob_x, wchar);
  90.                 rs_object[ii].ob_y = adj(rs_object[ii].ob_y, hchar);
  91.                 rs_object[ii].ob_width = adj(rs_object[ii].ob_width, wchar);
  92.                 rs_object[ii].ob_height = adj(rs_object[ii].ob_height, hchar);
  93.                 test = (int) rs_object[ii].ob_spec;
  94.                 switch (rs_object[ii].ob_type) {
  95.                         case G_TITLE:
  96.                         case G_STRING:
  97.                         case G_BUTTON:
  98.                                 fix_str(&rs_object[ii].ob_spec);
  99.                                 break;
  100.                         case G_TEXT:
  101.                         case G_BOXTEXT:
  102.                         case G_FTEXT:
  103.                         case G_FBOXTEXT:
  104.                                 if (test != NIL)
  105.                                    rs_object[ii].ob_spec = 
  106.                                         (char *) &rs_tedinfo[test];
  107.                                 break;
  108.                         case G_ICON:
  109.                                 if (test != NIL)
  110.                                    rs_object[ii].ob_spec =
  111.                                         (char *) &rs_iconblk[test];
  112.                                 break;
  113.                         case G_IMAGE:
  114.                                 if (test != NIL)
  115.                                    rs_object[ii].ob_spec =
  116.                                         (char *) &rs_bitblk[test];
  117.                                 break;
  118.                         default:
  119.                                 break;
  120.                         }
  121.                 }
  122.         }
  123.  
  124. fix_tedinfo()
  125.         {
  126.         int     ii;
  127.  
  128.         for (ii = 0; ii < NUM_TI; ii++)
  129.                 {
  130.                 fix_str(&rs_tedinfo[ii].te_ptext);
  131.                 fix_str(&rs_tedinfo[ii].te_ptmplt);
  132.                 fix_str(&rs_tedinfo[ii].te_pvalid);
  133.                 }
  134.         }
  135.  
  136. fix_frstr()
  137.         {
  138.         int     ii;
  139.  
  140.         for (ii = 0; ii < NUM_FRSTR; ii++)
  141.                 fix_str(&rs_frstr[ii]);
  142.         }
  143.  
  144. fix_str(where)
  145.         long    *where;
  146.         {
  147.         if (*where != NIL)
  148.           *where = (long) rs_strings[(int) *where];
  149.         }
  150.  
  151. fix_iconblk()
  152.         {
  153.         int     ii;
  154.  
  155.         for (ii = 0; ii < NUM_IB; ii++)
  156.                 {
  157.                 fix_img(&rs_iconblk[ii].ib_pmask);
  158.                 fix_img(&rs_iconblk[ii].ib_pdata);
  159.                 fix_str(&rs_iconblk[ii].ib_ptext);
  160.                 }
  161.         }
  162.  
  163. fix_bitblk()
  164.         {
  165.         int     ii;
  166.  
  167.         for (ii = 0; ii < NUM_BB; ii++)
  168.                 fix_img(&rs_bitblk[ii].bi_pdata);
  169.         }
  170.  
  171. fix_frimg()
  172.         {
  173.         int     ii;
  174.  
  175.         for (ii = 0; ii < NUM_FRIMG; ii++)
  176.                 fix_bb(&rs_frimg[ii]);
  177.         }
  178.  
  179. fix_bb(where)
  180.         long    *where;
  181.         {
  182.         if (*where != NIL)
  183.           *where = (long) (char *) &rs_bitblk[(char) *where];
  184.         }
  185.         
  186. fix_img(where)
  187.         long    *where;
  188. {
  189.         if (*where != NIL)
  190.          *where = (long) (char *) rs_imdope[(int) *where].image;
  191. }
  192.  
  193. #undef adj
  194.                                                                                
  195.  
  196. main()
  197. {
  198.    appl_init ();              /* Initialize application.        */
  199.    open_vwork ();             /* Set up workstation.            */
  200.    rsc_fix();                 /* recreate resource file in-line */
  201.    do_dialog(SAMPLE,OK);       /* Go do the dialog box.          */
  202. /*   print_results(tree_addr);  / * Print user's choices.          */
  203.    button_wait();             /* Wait for mouse button.         */
  204.    v_clsvwk (handle);         /* Close virtual workstation.     */
  205.    appl_exit ();              /* Back to the desktop.           */
  206. }
  207.  
  208. open_vwork ()
  209. {
  210.    int i;
  211.  
  212.    /* Get graphics handle, initialize the GEM arrays and open  */
  213.    /* a virtual workstation.                                   */
  214.  
  215.    handle = graf_handle (&dum,&dum,&dum,&dum);
  216.    for (i=0; i<10; work_in[i++] = 1);
  217.    work_in[10] = 2;
  218.    v_opnvwk (work_in, &handle, work_out);
  219. }
  220.  
  221. do_dialog ( rsc_tree , exit_obj)
  222. int    rsc_tree;
  223. int    exit_obj;
  224. {
  225.    int choice; /* Button choice from dialog. */
  226.  
  227.    /* Here we load the resource file.  If the file is missing,  */
  228.    /* we warn the user with an alert box then terminate the     */
  229.    /* program by skipping the code following the else.          */
  230.    /*   if (! rsrc_load ("\SAMPLE.RSC"))
  231.    form_alert (1, "[1][SAMPLE.RSC missing!][I'll do better!]"); */
  232.    /* If the resource file loads OK, we get the address of the  */
  233.    /* tree, get the coords for centering the dialog, save the   */
  234.    /* portion of the screen that'll be covered by the dialog,   */
  235.    /* and draw the dialog.  The mouse pointer is changed to     */
  236.    /* pointing finger.                                          */
  237.    /*  else {                                                   */
  238.    /*  rsrc_gaddr (R_TREE, SAMPLE, &tree_addr);                 */
  239.  
  240.       tree = (OBJECT *) rs_trindex[ rsc_tree ];
  241.       form_center (tree, &dial_x, &dial_y, &dial_w, &dial_h);
  242.       objc_offset (tree, NUMBERS, &n_x, &n_y);
  243.       form_dial (FMD_START, 0, 0, 10, 10, dial_x, dial_y, dial_w, dial_h);
  244.       form_dial (FMD_GROW, 0, 0, 10, 10, dial_x, dial_y, dial_w, dial_h);
  245.       objc_draw (tree, 0, 2, dial_x, dial_y, dial_w, dial_h);
  246.       graf_mouse (FINGER,&dum);
  247.  
  248.    /* Here we allow the user to interact with the dialog then,  */
  249.    /* based on the chosen button, perform the necessary action. */
  250.    /* The form_do function is repeated until the user chooses   */
  251.    /* either the OK button or the CANCEL button.                */
  252.  
  253.       num = 0;
  254.       do {
  255.          choice = form_do (tree, YOURNAME);
  256.          if (choice == RADIO1) v_gtext (handle,160,20,"Radio 1   ");
  257.          if (choice == RADIO2) v_gtext (handle,160,20,"Radio 2   ");
  258.          if (choice == RADIO3) v_gtext (handle,160,20,"Radio 3   ");
  259.          if (choice == RADIO4) v_gtext (handle,160,20,"Radio 4   ");
  260.          if (choice == RADIO5) v_gtext (handle,160,20,"Radio 5   ");
  261.          if (choice == RADIO6) v_gtext (handle,160,20,"Radio 6   ");
  262.          if (choice == OPTION1) v_gtext (handle,160,20,"Option 1  ");
  263.          if (choice == OPTION2) v_gtext (handle,160,20,"Option 2  ");
  264.          if (choice == UPARO) do_up();
  265.          if (choice == DNARO) do_down();
  266.       }
  267.       while (choice != CANCEL && choice != exit_obj);
  268.  
  269.    /* Once the CANCEL or OK buttons have been pressed, we clean  */
  270.    /* up after ourselves by performing the "shrinking box" and   */
  271.    /* then redrawing the screen.                                 */
  272.  
  273.       form_dial (FMD_SHRINK, 0, 0, 10, 10, dial_x, dial_y, dial_w, dial_h);
  274.       form_dial (FMD_FINISH, 0, 0, 10, 10, dial_x, dial_y, dial_w, dial_h);
  275.       print_results();                /* Print user's choices. */
  276. }
  277.  
  278. do_up ()
  279. {
  280.  
  281.    /* First we increment our value and make sure it stays in    */
  282.    /* range.  If the value has become larger than 9999, we must */
  283.    /* also reinitialize display string for the object NUMBERS.  */
  284.    /* We then call our function to update the NUMBERS object.   */
  285.  
  286.    num += 1;
  287.    if (num > 9999) {
  288.       num = 0;
  289.       strcpy (number_str,"    0000    ");
  290.    }
  291.    edit_object ();
  292. }
  293.  
  294. do_down ()
  295. {
  296.  
  297.    /* Here we decrement the value and check for its range, after */
  298.    /* which we update the NUMBERS object.                        */
  299.  
  300.    num -= 1;
  301.    if (num < 0) num = 9999;
  302.    edit_object ();
  303. }
  304.  
  305. edit_object ()
  306. {
  307.    TEDINFO *ob_tedinfo;
  308.    char temp_str[10];
  309.  
  310.    /* Here we edit the string we're using for the text display  */
  311.    /* in the object NUMBERS so that it reflects the new value.  */
  312.  
  313.    sprintf (temp_str,"%d",num);
  314.    strcpy (&number_str[8 - strlen (temp_str)],temp_str);
  315.    strcpy (&number_str[8],"    ");
  316.  
  317.    /* Then we find the object NUMBERS' TEDINFO and point the    */
  318.    /* te_ptext member to our updated string, after which we     */
  319.    /* redraw the object NUMBERS.                                */
  320.  
  321.    ob_tedinfo = (TEDINFO *) tree[NUMBERS].ob_spec;
  322.    ob_tedinfo -> te_ptext = number_str;
  323.    objc_draw (tree, NUMBERS, 1, n_x, n_y, 96, 16);
  324. }
  325.  
  326. print_results()
  327. {
  328.    char *string;
  329.  
  330.    /* Here we call the function that locates the string, then  */
  331.    /* print the user's input to the screen.                    */
  332.  
  333.    string = find_str (YOURNAME, string);
  334.    v_gtext (handle, 160, 20, "Your name is ");
  335.    v_gtext (handle,264,20,string);
  336.    string = find_str (AGE, string);
  337.    v_gtext (handle, 160, 28, "Your age is ");
  338.    v_gtext (handle, 264, 28, string);
  339.    string = find_str (NUMBERS, string);
  340.    v_gtext (handle, 160, 36, "Final number value: ");
  341.    v_gtext (handle,320,36,&string[4]);
  342. }
  343.  
  344. char *find_str (object, string)
  345. int object;
  346. char *string;
  347. {
  348.    TEDINFO *ob_tedinfo;
  349.  
  350.    /* In this function, we locate the object's TEDINFO structure */
  351.    /* then set our string pointer to the pointer found in the    */
  352.    /* te_ptext member.                                           */
  353.  
  354.    ob_tedinfo = (TEDINFO *) tree[object].ob_spec;
  355.    string = ob_tedinfo -> te_ptext;
  356.    return (string);
  357. }
  358.    
  359. /* Waits for left button to be pressed and released. */
  360. button_wait()
  361. {
  362.     evnt_button (1,1,1,&dum,&dum,&dum,&dum);
  363.     evnt_button (1,1,0,&dum,&dum,&dum,&dum);
  364. }
  365. /*
  366. void    sort(v,n)
  367. char    *v[];
  368. int     n;
  369.         {
  370.         .
  371.         .
  372.         }
  373. .
  374. .
  375. .
  376. void    do_form( rsc_tree, exit_obj )
  377. int     rsc_tree;
  378. int     exit_obj;
  379.         {
  380.         int     xdial, ydial, wdial, hdial;
  381.         int     x, y, w, h;
  382.         int     object;
  383.  
  384.         tree = (OBJECT *) rs_trindex[ rsc_tree ];
  385.  
  386.         form_center( tree, &xdial, &ydial, &wdial, &hdial );
  387.         x = xdial + wdial/2;
  388.         y = ydial + hdial/2;
  389.         w = gl_wbox;
  390.         h = gl_hbox;
  391.         form_dial( 0, x, y, w, h, xdial, ydial, wdial, hdial );
  392.         form_dial( 1, x, y, w, h, xdial, ydial, wdial, hdial );
  393.  
  394.         objc_draw( tree, ROOT, MAX_DEPTH, xdial, ydial, wdial, hdial );
  395.  
  396.         object = 0;
  397.  
  398.         while( object != exit_obj )
  399.                 {
  400.                 object = form_do( tree, 0 ) & 0x7FFF;
  401.                 handle_object( tree, object );
  402.                 .
  403.                 .
  404.                 .
  405.                 }
  406.  
  407.         form_dial( 2, x, y, w, h, xdial, ydial, wdial, hdial );
  408.         form_dial( 3, x, y, w, h, xdial, ydial, wdial, hdial );
  409.         }
  410.  
  411. main()
  412.         {
  413.         if ( appl_init() >= 0 )
  414.                 {
  415.                 rsc_fix();
  416.                 .
  417.                 .
  418.                 graf_mouse( M_ON, 0x0L );
  419.                 graf_mouse( ARROW, 0x0L );
  420.                 do_form( TREE0, OKBUTTON );
  421.                 .
  422.                 .
  423.                 appl_exit();
  424.                 }
  425.         }
  426. */                                                          
  427.